package com.lrm.web;

import com.lrm.po.User;
import com.lrm.service.UserService;
import com.lrm.util.AliOSSUtils;
import com.lrm.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    AliOSSUtils aliOSSUtils;


    @PostMapping("/doupdate")
    public String update(HttpServletRequest request, @RequestParam("fileName")MultipartFile image) throws IOException {
        HttpSession session = request.getSession();
        String email = request.getParameter("email");
        String nickname = request.getParameter("nickname");
        String password = request.getParameter("password");
        User user = (User) session.getAttribute("user");
        if(!image.isEmpty()){
            String url = aliOSSUtils.upload(image);//返回值就是访问文件的url
            user.setAvatar(url);
        }
        if(!email.isEmpty()){
            System.out.println("email is Not empty");
            user.setEmail(email);
        }
        else{
            System.out.println("email is empty");
            user.setEmail(null);
        }
        if(!nickname.isEmpty())
            user.setNickname(nickname);
        else
            user.setNickname(null);

        if(!password.isEmpty())
            user.setPassword(MD5Utils.code(password));
        else
            user.setPassword(null);

        userService.update(user);
        return "admin/index";
    }
}
